# *.dll file must be placed into directory [C:\Windows\System32]
# Or add *.dll file' path to System environment variable [Path]
#################################################################################################### 
# -> C-dll function declare form ->
# [DllImport("*.dll")]public static extern _RETVAL_TYPE fx_name(_ARG_TYPE arg, _ARG_TYPE arg, ... );
#                     ^
#                     |
# Attention: NO blank or New-line character between ']' and 'public' 
####################################################################################################
$fx0_load = @"
[DllImport("libfx0.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern int int_add(int para1, int para2);
[DllImport("libfx0.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern void Scalars(double invar, ref double outvar);
[DllImport("libfx0.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern void OneDArray(int n, double [] anArray);
[DllImport("libfx0.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern void TwoDArray(int m, int n, double [,] a2dArray, int tda);   
"@ 

$fx0_free = @" 
[DllExport("libfx0.dll", CallingConvention = CallingConvention.Cdecl)]
"@ 

$FX0 = Add-Type -MemberDefinition $fx0_load -Name "libfx0" -Namespace libfx0 -PassThru
#                                                    ^                  ^
#                                                    |                  |
#                             Name of class(C library name)      library name for C-dll

[int]$i1=2; [int]$i2=99; 
$sum=$FX0::int_add($i1, $i2)
Write-Host "Cdll function: int_add($i1, $i2);" "Return: $sum"

[double]$in=1.3; [double]$ret=0.0; ### double variable must be initialized!
$FX0::Scalars($in, [ref]$ret)  ### C# reference equal C pointer.
Write-Host "Cdll function: Scalars($in, [ref]ret);" "ret: $ret"

[int]$n=2; 
[double[]]$anArray=@(0.0)*20;  ### double Array(20 elements) initialized
#echo $anArray.GetLongLength(0)
$FX0::OneDArray($n, $anArray)
Write-Host  "Cdll function: OneDArray(n, anArray); ret:" $anArray[0] $anArray[1]

[int]$m=3;[int]$n=4; 
[double[,]]$a2dArray=New-Object 'double[,]'$m,$n;  ### double two-dimensional(m*n elements) initialized
Write-Host "Row:"$a2dArray.GetLength(0) "Col:"$a2dArray.GetLength(1)
$FX0::TwoDArray($a2dArray.GetLength(0), $a2dArray.GetLength(1), $a2dArray, $a2dArray.GetLength(1))
for($i=0;$i-lt$m;$i++){
 for($j=0;$j-lt$n;$j++){ Write-Host -NoNewline "    "$a2dArray[$i,$j] }
 Write-Host
}

